<?php
require_once ('ini_set.inc');
require_once ('LyException.php');
require_once ('db.php');
require_once ('ClassUtil.php');
require_once ('GenerateObj.php');
class User  extends GenerateObj{
	
	protected $fields=array("id","email","name","password","sex","image","credit","create_time");
	protected $table_name="user";
	public $id;
	public $email;
	public $name;
	public $password;
	public $sex;
	public $image;
	public $credit;
	public $create_time;
	//public $user_category ;

	public function __construct(){
		$args=func_get_args();
		ClassUtil::callMethodForArgs($this,$args);
	}
	public function construct_(){
	}
	public  function construct_number($num){
		$this->id=$num;
		$this->generate_obj_by_field("id",$num);
	}
	public  function construct_string($string){
		$this->email=$string;
		$this->generate_obj_by_field("email",$string);
	}
	public function construct_string_string_string_number_string_number($email,$name,$password,$sex,$image,$credit){
		$this->email=$email;
		$this->name=$name;
		$this->password=$password;
		$this->sex=$sex;
		$this->image=$image;
		$this->credit=$credit;
	}
	private function db_save_user($email,$name,$password,$sex,$image,$credit){
		$sql="insert into user (email,name,password,sex,image,credit) values ('$email','$name','$password','$sex','$image','$credit')";
		$query = db_query($sql, "users", "write");
		if(!mysql_insert_id()) throw new LyException("save user fails");
		return mysql_insert_id();
	}
	public function save(){
		$query_email_exist=$this->db_get_obj_by_field("email",$this->email);
		if($query_email_exist>0)	return ;
		$id=$this->db_save_user($this->email,$this->name,$this->password,$this->sex,$this->image,$this->credit);
		$this->id=$id;
		$this->create_time=date("Y-m-d H:i:s");
	}
}
?>